function [audio_data,intp_data,bitstream_data,vout_data] = dac_top(root_path_str ,input_data,input_fs)
%% 输入参数
% root_path_str       : 设置将dac_top代码文件存放的路径
% input_data          : 输入的数据,只要这个输入不为空,DAC_TOP就会优先使用外部输入数据而不是内部产生的激励信号.
%                       需要注意,DAC_TOP在用的时候会对input_data截取2^N个数,N取决于length(input_data)的大小
%                       会自动计算N的最大值
% input_fs            : 给输入数据指定采样率

%% 输出参数
% audio_data          : 经处理的输入数据,因DAC_TOP会截取输入数据,所以该输出并不在所有情况下等于输入
% intp_data           : 插值后的数据
% bitstream_data      : 经sigma-delta调制后的调制器输出
% vout_data           : 经模拟DAC和LPF滤波后的输出数据

%%
    if nargin ==0
        root_path_str = 'E:\WorkSpace\2025\Audio\sddac\matlab_sim';
        input_data = [];
        input_fs = [];
    end
    if nargin == 1
        input_data = [];
        input_fs = [];
    end
    if nargin == 2
        error("输入数据采样率不能为空");
    end
    if nargin == 3 && isempty(input_fs)
        error("输入数据采样率不能为空");
    end

    str_version = "V6 2025-09-18 10:38";
    u_sdm_p1 = cfg_param_struct();
    u_sdm_p1.addpath_str.root_path = root_path_str;

    addpath([root_path_str,u_sdm_p1.addpath_str.str1       ]);
    addpath([root_path_str,u_sdm_p1.addpath_str.str2       ]);
    addpath([root_path_str,u_sdm_p1.addpath_str.str3       ]);
    addpath([root_path_str,u_sdm_p1.addpath_str.str4       ]);
    addpath([root_path_str,u_sdm_p1.addpath_str.str5       ]);
    addpath([root_path_str,u_sdm_p1.addpath_str.str6       ]);
    addpath([root_path_str,u_sdm_p1.addpath_str.str7       ]);
    addpath([root_path_str,u_sdm_p1.addpath_str.str8       ]);


%% 获取原始数据
    if(isempty(input_data)==true)
        % --------- 生成数据 ---------
        if(u_sdm_p1.raw_data_cfg.type == "file")
            [audio_data] = create_audio_raw_data(u_sdm_p1.raw_data_cfg.create_audio_raw_data);
        else
            [audio_data] = generate_mixed_sine(u_sdm_p1.raw_data_cfg.generate_mixed_sine);
        end

        % --------- 定点化 ---------
        audio_data = fixed_point_calculation_vector(audio_data , 1, 'mul' , u_sdm_p1.fixed_point_cfg.Int_num , u_sdm_p1.fixed_point_cfg.Fixed_num,u_sdm_p1.fixed_point_cfg.DataType);

        % --------- 图像分析 ---------
        if(u_sdm_p1.debug.raw_data_cfg_CustomSpectrum_en == "enable")
            plotCustomSpectrum_method2(audio_data,u_sdm_p1.raw_data_cfg.raw_fs,1,"custom",'原始数据 频谱图');


%             filename = '23k_sin_data.txt';
%             fid = fopen(filename, 'wt');
%             if fid == -1
%                 error('无法创建或打开文件：%s。请检查文件名是否有效或是否有写入权限。', filename);
%             end
%             fprintf(fid, '%f\n', fix(audio_data*2^23));
%             fclose(fid);
%             fprintf('数组已成功写入文件：%s\n', filename);



        end
        if(u_sdm_p1.debug.raw_data_cfg_PowerSpectrum_en == "enable")
            plotPowerSpectrum(audio_data,u_sdm_p1.raw_data_cfg.pn,u_sdm_p1.raw_data_cfg.raw_fs,1,'原始数据 功率谱图');
        end
    else
        u_sdm_p1.raw_data_cfg.pn = 2^fix(log2(length(input_data)));
        u_sdm_p1.raw_data_cfg.raw_fs = input_fs;
        audio_data = input_data(1:u_sdm_p1.raw_data_cfg.pn);
    end
    %clc;
    fprintf("加载原始数据已完毕.\n");

    %% 数据处理


    [bitstream_data  ,vout_data  ,intp_data   ]  = sigma_delta_dac_top(audio_data,u_sdm_p1);

    % --------- 定点化 ---------
    bitstream_data   = fixed_point_calculation_vector(bitstream_data , 1, 'mul' , u_sdm_p1.fixed_point_cfg.Int_num , u_sdm_p1.fixed_point_cfg.Fixed_num,u_sdm_p1.fixed_point_cfg.DataType);
    vout_data        = fixed_point_calculation_vector(vout_data , 1, 'mul' , u_sdm_p1.fixed_point_cfg.Int_num , u_sdm_p1.fixed_point_cfg.Fixed_num,u_sdm_p1.fixed_point_cfg.DataType);
    intp_data        = fixed_point_calculation_vector(intp_data , 1, 'mul' , u_sdm_p1.fixed_point_cfg.Int_num , u_sdm_p1.fixed_point_cfg.Fixed_num,u_sdm_p1.fixed_point_cfg.DataType);

%% 结果分析

    % 调制器bit流信号频谱分析
    if(u_sdm_p1.debug.result_check_custom_intp=="enable")
        plotCustomSpectrum_method2(intp_data,u_sdm_p1.raw_data_cfg.raw_fs,u_sdm_p1.sdm.osr,"custom",'插值输出 频谱图');
    end
    if(u_sdm_p1.debug.result_check_custom_bitstream=="enable")
        plotCustomSpectrum_method2(bitstream_data,u_sdm_p1.raw_data_cfg.raw_fs,u_sdm_p1.sdm.osr,"custom",'码流输出 频谱图');
    end
    if(u_sdm_p1.debug.result_check_custom_vout=="enable")
        plotCustomSpectrum_method2(vout_data,u_sdm_p1.raw_data_cfg.raw_fs,u_sdm_p1.sdm.osr,"custom",'DAC输出 频谱图');
    end

    if(u_sdm_p1.debug.result_check_power_intp=="enable")
        plotPowerSpectrum(intp_data,u_sdm_p1.raw_data_cfg.pn,u_sdm_p1.raw_data_cfg.raw_fs,u_sdm_p1.sdm.osr,'插值输出 功率谱图');
    end
    if(u_sdm_p1.debug.result_check_power_bitstream=="enable")
        plotPowerSpectrum(bitstream_data,u_sdm_p1.raw_data_cfg.pn,u_sdm_p1.raw_data_cfg.raw_fs,u_sdm_p1.sdm.osr,'码流输出 功率谱图');
    end
    if(u_sdm_p1.debug.result_check_power_vout=="enable")
        plotPowerSpectrum(vout_data,u_sdm_p1.raw_data_cfg.pn,u_sdm_p1.raw_data_cfg.raw_fs,u_sdm_p1.sdm.osr,'DAC输出 功率谱图');
    end

    if(u_sdm_p1.debug.result_check_snr_intp=="enable")
        CalculateSNR(intp_data,u_sdm_p1.raw_data_cfg.pn,u_sdm_p1.raw_data_cfg.raw_fs,u_sdm_p1.sdm.osr,u_sdm_p1.raw_data_cfg.generate_mixed_sine.freqs,u_sdm_p1.debug.result_check_snr_main_band_limit,'插值输出 SNR');
    end
    if(u_sdm_p1.debug.result_check_snr_bitstream=="enable")
        CalculateSNR(bitstream_data,u_sdm_p1.raw_data_cfg.pn,u_sdm_p1.raw_data_cfg.raw_fs,u_sdm_p1.sdm.osr,u_sdm_p1.raw_data_cfg.generate_mixed_sine.freqs,u_sdm_p1.debug.result_check_snr_main_band_limit,'码流输出 SNR');
    end
    if(u_sdm_p1.debug.result_check_snr_vout=="enable")
        CalculateSNR(vout_data,u_sdm_p1.raw_data_cfg.pn,u_sdm_p1.raw_data_cfg.raw_fs,u_sdm_p1.sdm.osr,u_sdm_p1.raw_data_cfg.generate_mixed_sine.freqs,u_sdm_p1.debug.result_check_snr_main_band_limit,'DAC输出 SNR');
    end

    fprintf("%s \n", str_version);
end




function param_return = cfg_param_struct()

%% 路径字符串
    addpath_str.str1                                        = '\Include\DAC'                    ;
    addpath_str.str2                                        = '\Include\参数类型'               ;
    addpath_str.str3                                        = '\Include\DAC\过采样插值'         ;
    addpath_str.str4                                        = '\Include\DAC\数据来源'           ;
    addpath_str.str5                                        = '\Include\DAC\频谱分析'           ;
    addpath_str.str6                                        = '\Include\DAC\定点化计算'         ;
    addpath_str.str7                                        = '\Include\DAC\SDM'               ;
    addpath_str.str8                                        = '\Include\DAC\模拟DAC'               ;
    addpath_str.root_path                                   =[];


%%  原始数据采样配置

    % raw_data_cfg.type                 选项 - file :数据来自于读文件  - sim :数据来自于生成的激励信号
    raw_data_cfg.type                                       = "sim"                     ;


    raw_data_cfg.pn                                         = 2^16                      ;
    raw_data_cfg.raw_fs                                     = 48000                     ;

        create_audio_raw_data.pn                            = raw_data_cfg.pn         ;
        create_audio_raw_data.raw_fs                        = raw_data_cfg.raw_fs     ;

        generate_mixed_sine.pn                              = raw_data_cfg.pn         ;
        generate_mixed_sine.raw_fs                          = raw_data_cfg.raw_fs     ;
        generate_mixed_sine.freqs                           = [ 15000                ];
        generate_mixed_sine.amps                            = [ 1                   ];

    raw_data_cfg.create_audio_raw_data                      = create_audio_raw_data  ;
    raw_data_cfg.generate_mixed_sine                        = generate_mixed_sine  ;

%%  sigma_delta DAC 配置
    sdm.osr                                                 = 128       ;
    sdm.method                                              = "fir"      ;
    sdm.bitlevel                                            = 4;
    sdm.order                                               = "4th order ciff";

    sdm.vref_val                                            = 1.0       ;
    sdm.miss_en                                             = "enable" ;
    sdm.shap_en                                             = "enable" ;
    sdm.msh_level                                           = "worst"   ;
    sdm.gwn_en                                              = "ednable"  ;

%% 定点/浮点数配置
    fixed_point_cfg.DataType    = "Fixed"   ;
    fixed_point_cfg.Int_num     = 9         ;
    fixed_point_cfg.Fixed_num   = 23        ;

%% 调试相关
    debug.raw_data_cfg_CustomSpectrum_en                            = "enable"                 ;
    debug.raw_data_cfg_PowerSpectrum_en                             = "disable"                 ;
    debug.sdm_plot_en                                               = "disable"                 ;
    debug.sdm_internal_sig                                          = "denable"                 ;
    debug.intp_input_raw_data                                       = "disable"                 ;
    debug.intp_insert_0_data                                        = "enable"                 ;
    debug.intp_fir_out_data                                         = "enable"                 ;
    debug.intp_line_out_data                                        = "enable"                 ;
    debug.result_check_custom_bitstream                             = "enable"                  ;
    debug.result_check_custom_vout                                  = "disable"                 ;
    debug.result_check_custom_intp                                  = "disable"                 ;
    debug.result_check_power_bitstream                              = "disable"                 ;
    debug.result_check_power_vout                                   = "enable"                  ;
    debug.result_check_power_intp                                   = "disable"                 ;
    debug.result_check_snr_intp                                     = "disable"                 ;
    debug.result_check_snr_bitstream                                = "disable"                 ;
    debug.result_check_snr_vout                                     = "enable"                  ;
    debug.result_check_snr_main_band_limit                          = 20000                     ;
%%  模块集合
    param_return.raw_data_cfg                       = raw_data_cfg          ;
    param_return.fixed_point_cfg                    = fixed_point_cfg       ;
    param_return.sdm                                = sdm                   ;
    param_return.debug                              = debug                 ;
    param_return.addpath_str                        = addpath_str           ;
end
